<?php


namespace App\Http\Controllers\merchant;

use App\Common\Components\ApiController;
use App\Models\Merchant\MerchantRole;
use App\Models\Merchant\MerchantRoleMenuRelation;
use App\Models\Merchant\MerchantMenuRelation;
use App\Models\Merchant\MerchantMenu;
use Illuminate\Support\Facades\DB;

class RoleController extends ApiController
{
    
    public function listAll(){
        $record = MerchantRole::get();
        return $this->successJson('操作成功',$record);
    }

    public function list(){
        $record = MerchantRole::paginate(10);
        return $this->successJson('操作成功',$record);
    }

    public function updateStatus(){
        $param = request()->input();
        $role_id = $param['id'];
        $status = $param['status'];
        $role = MerchantRole::where('id',$role_id)->first();
        $role->status = $status;
        $role->save();
        return $this->successJson();
    }

    public function update(){
        $param = request()->input();
        $role_id = $param['id'];
        $role = MerchantRole::where('id',$role_id)->first();
        $role->name = $param['name'];
        $role->description = $param['description'];
        $role->admin_count = $param['admin_count'];
        $role->status = $param['status'];
        $role->sort = $param['sort'];
        $role->save();
        return $this->successJson();
    }

    
    public function create(){
        $param = request()->input();
        $role = new MerchantRole();
        $role->fill($param);
        $now = new \DateTime();
        $role->create_time = $now;
        $role->save();
        return $this->successJson();
    }

    public function delete(){
        $param = request()->input();
        $role_id = $param['id'];
        MerchantRole::where('id', $role_id)->delete();
        return $this->successJson();
    }

    public function allocMenu(){
        $param = request()->input();
        $RoleMenuRelation = MerchantRoleMenuRelation::where('role_id', $param['roleId'])->first();
        if($RoleMenuRelation){
            $RoleMenuRelation->menu_code = $param['menuCodes'];
            $RoleMenuRelation->save();
        }else{
            MerchantRoleMenuRelation::create([
                'role_id' => $param['roleId'],
                'menu_code' =>$param['menuCodes'],
            ]);
        }
        return $this->successJson();
    }

    public function listMenu(){
        $param = request()->input();
        $role_id = $param['roleId'];
        $merchant_id = $_SERVER['HTTP_AUTHORIZATION'];
        $RoleMenuRelation = MerchantRoleMenuRelation::where('role_id', $role_id)->first();
        $menuCodes = explode(",", $RoleMenuRelation->menu_code);
        $data['menuCodes'] = $menuCodes;
        //查出未授权的菜单
        $MerchantMenuRelation = MerchantMenuRelation::where('merchant_id',$merchant_id)->first();
        $menu_code = explode(",", $MerchantMenuRelation->menu_code);
        $MerchantMenu = MerchantMenu::get();
        $noMenus = [];
        foreach($MerchantMenu as $item){
            if(!in_array($item->menu_code, $menu_code)){
                $noMenus[] = $item->menu_code;
            }
        }
        $data['menuCodes'] = $menuCodes;
        $data['noMenus'] = $noMenus;
        return $this->successJson('',$data);
    }
}